<?php
switch($action) {
case listUser;
	listUser();
	break;
case add;
	add();
	break;
case delete;
	delete();
	break;
case edit;
	edit();
	break;
case apparatus;
	apparatus();
	break;
case department;
	department();
	break;
case addDepartment;
	addDepartment();
	break;
case deleteDepartment;
	deleteDepartment();
	break;
case editDepartment;
	editDepartment();
	break;
case addApparatus;
	addApparatus();
	break;
case deleteApparatus;
	deleteApparatus();
	break;
case editApparatus;
	editApparatus();
	break;
case editmodule;
	editModule();
	break;
}



function editModule() {
	global $config,$userInfo;
	if(_g('submit')) {
		$moduleid = _g('moduleid');
		$data['role'] = @implode("|",$moduleid);
		$uid = intval(_g('uid'));
		$user = new User();
		$data = $user->saveEdit($data,$uid);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$Role = new Role();
		$userInfo['role'] = explode("|", $userInfo['role']);
		$data = $Role->roleTreeSort();
		foreach ($data as $key=>$value){
			if($value['pid'] == 0) {
				$dataArray[] = $value;
			}
		}
		foreach ($dataArray as $key=>&$value){
			$value['Children'] = $Role->Rolefzid($value['id']);
		}
	}
	include template('manage/UserEditGroup');
}

function apparatus() {
	global $config,$userInfo;
	$Role = new Role();
	$page = intval(_g('page'));
	$num = 20;
	$apparatus = $Role->getApparatusList($page,$num,$findkey='',$order='jid',$fields='*');
	include template('manage/apparatus');
}

function addApparatus() {
	global $config,$userInfo;
	if(_g('submit')) {
		$data['names'] = trim(_g('names'));
		if(empty($data['names'])) {
			$message = array(
				'status' => 'error',
				'msg' => '机构名称不能为空哦',
			);
			echo json_encode($message);
			exit;
		}
		$data['infos'] = trim(_g('infos'));
		if(empty($data['infos'])) {
			$message = array(
				'status' => 'error',
				'msg' => '机构简介不能为空哦',
			);
			echo json_encode($message);
			exit;
		}
		$Role = new Role();
		$data['times'] = time();
		$saveDepartment = $Role->saveApparatus($data);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		include template('manage/addApparatus');
	}
}



function editApparatus() {
	global $config,$userInfo;
	if(_g('submit')) {
		$data['names'] = trim(_g('names'));
		$jid = intval(_g('jid'));
		if(empty($jid)) {
			$massage = array(
					'status' => 'error',
					'msg' => '未知错误，不知道怎么来的哦'
				);
		}
		if(empty($data['names'])) {
			$message = array(
				'status' => 'error',
				'msg' => '机构名称不能为空哦',
			);
			echo json_encode($message);
			exit;
		}
		$data['infos'] = trim(_g('infos'));
		$Role = new Role();
		$Role->saveEditApparatus($data,$jid);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$jid = intval(_g('jid'));
		$Role = new Role();
		$findkey = 'jid = '.$jid;
		$Apparatus = $Role->getApparatusRow($findkey);
		include template('manage/editApparatus');
	}
}


function deleteApparatus() {
	global $config,$userInfo;
	$jid = intval(_g('jid'));
	$Role = new Role();
	$Role -> deleteApparatus($jid);
	$message = array(
			'status' =>'success'
	);
	echo json_encode($message);
	exit;
}


function department() {
	global $config,$userInfo;
	$Role = new Role();
	$num = 20;
	$departmentUser = $Role->departmentListUser($page,$num,$findkey='',$order='bid',$fields='*');
	include template('manage/department');
}



function addDepartment() {
	global $config,$userInfo;
	if(_g('submit')) {
		$data['names'] = trim(_g('names'));
		if(empty($data['names'])) {
			$message = array(
				'status' => 'error',
				'msg' => '部门名称不能为空哦',
			);
			echo json_encode($message);
			exit;
		}
		$data['infos'] = trim(_g('infos'));
		if(empty($data['infos'])) {
			$message = array(
				'status' => 'error',
				'msg' => '部门简介不能为空哦',
			);
			echo json_encode($message);
			exit;
		}
		$data['jid'] = intval(_g('jid'));
		$Role = new Role();
		$findkey[] = 'jid = '.$data['jid'];
		$jname = $Role->getApparatusRow($findkey,'names');
		if(empty($jname)) {
			$message = array(
				'status' => 'error',
				'msg' => '哦，系统出错了,这是为什么呢？',
			);
			echo json_encode($message);
			exit;
		}
		$data['jname'] = $jname['names'];
		$data['times'] = time();
		$saveDepartment = $Role->saveDepartment($data);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$Role = new Role();
		$apparatusList = $Role->getApparatusList(0,50,'',$order='jid',$fields='*');
		include template('manage/addDepartment');
	}
}



function editDepartment() {
	global $config,$userInfo;
	if(_g('submit')) {
		$data['names'] = trim(_g('names'));
		$bid = intval(_g('bid'));
		if(empty($data['names'])) {
			$message = array(
				'status' => 'error',
				'msg' => '部门名称不能为空哦',
			);
			echo json_encode($message);
			exit;
		}
		$data['infos'] = trim(_g('infos'));
		if(empty($data['infos'])) {
			$message = array(
				'status' => 'error',
				'msg' => '部门简介不能为空哦',
			);
			echo json_encode($message);
			exit;
		}
		$data['jid'] = intval(_g('jid'));
		$Role = new Role();
		$findkey[] = 'jid = '.$data['jid'];
		$jname = $Role->getApparatusRow($findkey,'names');
		if(empty($jname)) {
			$message = array(
				'status' => 'error',
				'msg' => '哦，系统出错了,这是为什么呢？',
			);
			echo json_encode($message);
			exit;
		}
		$data['jname'] = $jname['names'];
		$data['times'] = time();
		$saveDepartment = $Role->saveEditDepartment($data,$bid);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$bid = intval(_g('bid'));
		$Role = new Role();
		$findkey = 'bid = '.$bid;
		$Department = $Role->getDepartmentRow($findkey);
		$apparatusList = $Role->getApparatusList(0,50,'',$order='jid',$fields='*');
		include template('manage/editDepartment');
	}
}


function deleteDepartment() {
	global $config,$userInfo;
	$bid = intval(_g('bid'));
	$Role = new Role();
	$Role -> deleteDepartment($bid);
	$message = array(
			'status' =>'success'
	);
	echo json_encode($message);
	exit;
}



function listUser() {
	global $config,$userInfo;
	$user = new User();
	$page = intval(_g('page'));
	$num = 20;
	$userList = $user->getUser($page,$num,$findkey='',$order='uid',$fields='*');
	include template('manage/listUser');
}


function add() {
	global $config,$userInfo;
	if(_g('submit')) {
		$data['username'] = trim(_g('username'));
		$user = new User();
		$findkey = "username ='".$data['username']."'";
		$erro = $user->getUser(1,1,$findkey,$order='uid',$fields='*');
		if(!empty($erro)) {
			$message = array(
				'status' => 'error',
				'msg' => '用户名有冲突:请更换一个登录名称',
			);
			echo json_encode($message);
			exit;
		}
		unset($findkey);
		$data['userPassword'] = md5(trim(_g('password')));
		$data['roleid'] = intval(_g('groupid'));
		$data['jid'] = intval(_g('jid'));
		$data['bid'] = intval(_g('bid'));
		$data['email'] = trim(_g('email'));
		$findkey = "email ='".$data['email']."'";
		$erro = $user->getUser(1,1,$findkey,$order='uid',$fields='*');
		if(!empty($erro)) {
			$message = array(
				'status' => 'error',
				'msg' => '邮箱有冲突:请更换一个电子邮箱',
			);
			echo json_encode($message);
			exit;
		}
		$Role = new Role();
		$roleArray = $Role->seachRole($data['roleid']);
		if(empty($roleArray)) {
			$message = array(
				'status' => 'error',
				'msg' => '未知错误，请刷新页面',
			);
			echo json_encode($message);
			exit;
		}
		$data['roleName'] = $roleArray['roleName'];
		$data = $user->saveReg($data);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$Role = new Role();
		$groupList = $Role->getRole(1,50,'',$order='id',$fields='*');
		$apparatusList = $Role->getApparatusList(1,50,'',$order='jid',$fields='*');
		$departmentList = $Role->getDepartmentList(1,50,'',$order='bid',$fields='*');
		include template('manage/addUser');
	}
}

function delete() {
	global $config,$userInfo;
	$id = intval(_g('id'));
	$user = new User();
	$roleArray = $user->delUser($id);
	$message = array(
		'status' => 'success',
	);
	echo json_encode($message);
	exit;
}

function edit() {
	global $config,$userInfo;
	if(_g('submit')) {
		$password = trim(_g('password'));
		if($password !='******') {
			$data['userPassword'] = $password;
		}
		$data['roleid'] = trim(_g('groupid'));
		$data['jid'] = intval(_g('jid'));
		$data['bid'] = intval(_g('bid'));
		$Role = new Role();
		$roleArray = $Role->seachRole($data['roleid']);
		if(empty($roleArray)) {
			$message = array(
				'status' => 'error',
				'msg' => '未知错误，请刷新页面',
			);
			echo json_encode($message);
			exit;
		}
		$data['roleName'] = $roleArray['roleName'];
		$user = new User();
		$id = intval(_g('id'));
		$data = $user->saveEdit($data,$id);
		$message = array(
			'status' => 'success',
		);
		echo json_encode($message);
		exit;
	}else{
		$id = intval(_g('id'));
		$user = new User();
		$userArray = $user->getUserInfo($id);
		$Role = new Role();
		$groupList = $Role->getRole(1,50,$findkey='',$order='id',$fields='*');
		$apparatusList = $Role->getApparatusList(1,50,'',$order='jid',$fields='*');
		$departmentList = $Role->getDepartmentList(1,50,'',$order='bid',$fields='*');
		include template('manage/editUser');
	}
}
?>